package com.aiit.controller;
import com.aiit.pojo.Consultation;
import com.aiit.pojo.Patient;
import com.aiit.pojo.ProcResult;
import com.aiit.pojo.User;
import com.aiit.service.impl.ConService;
import com.aiit.service.impl.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * ClassName:ConsultationController
 * Package:com.aiit.controller
 * Description:
 *
 * @Date:2020/4/13 13:37
 * @Author:xjs
 */
@Controller
public class ConsultationController {

    @Resource
    private ConService conService;
    @Resource
    private UserService userService;
    @GetMapping("/view/consultation")
    public String consultation(Model model,@RequestParam("username") String username) {
       int userid =userService.findNameByid(username);
        model.addAttribute("userid",userid);
        return "/view/consultation";
    }
    @PostMapping(value = "/consultation")
    @ResponseBody
    @Transactional
    public ProcResult checkConsultation(@RequestBody User user) {
            Consultation consultation = new Consultation();
            consultation.setUserid(user.getUserid());
            consultation.setContent(user.getConsultation().getContent());
            System.out.println(user);
          Patient patient =new Patient();
          patient.setUserid(user.getUserid());
          patient.setName(user.getPatient().getName());
          patient.setSex(user.getPatient().getSex());
          patient.setAge(user.getPatient().getAge());
          patient.setAddress(user.getPatient().getAddress());
            if(conService.updateUser(patient) && conService.updateUserContent(consultation)){
                System.out.println("更新成功");
                return  ProcResult.success();
            }


        System.out.println(user);
        return ProcResult.fail().bind("errors","姓名不存在");
    }
}
